import cryptoJs from 'crypto-js';
import { Vue } from "vue-property-decorator";
import { AppState } from '@/store/modules/app/typings'
import { CrudState } from '@/store/modules/crud/typings'
import { Base64 } from 'js-base64';
import { NOTE, INFO, SAVE, PAGE } from '@/store/modules/crud/keys';
import { Component } from "vue-property-decorator";
@Component
export default class CryptoDes extends Vue {
    splitHeight: any = null;
    editor: any = null;

    get _name(){ return NOTE; }
    get appState() { return this.$store.state.app as AppState; }
    get state() { return this.$store.state.crud[this._name] as CrudState; }
    get page() { return this.state.page; }
    get info(){ return this.state.info; }
    get appd() {return this.state.appd; }
    
    init(){
        this.$nextTick(() => {
            const footerHeight = (this.$refs['footer'] as any).$el.offsetHeight;
            const noteHeight = (this.$refs.note as any).offsetHeight; //- 42 - 2;
            const splitHeight = noteHeight - footerHeight;
            this.splitHeight = splitHeight  - 2;
            let noteEditSpaceHeight = splitHeight - 42;
            const noteHeiSess = window.sessionStorage.getItem('noteEditSpaceHeight');//将高度保存在session，解决刷新时高度改变的问题
            if(noteHeiSess){
                noteEditSpaceHeight = Number(noteHeiSess);
            }
            window.sessionStorage.setItem('noteEditSpaceHeight',String(noteEditSpaceHeight));
            if(this.editor== undefined || this.editor==null){ 
                return;
            }
            this.editor.config.zIndex = 99;
            this.editor.config.placeholder = '欢迎使用编辑器...';
            this.editor.config.uploadImgShowBase64 = true;
            this.editor.config.onchangeTimeout = 5000; // 配置触发 onchange 的时间频率，默认为200ms ,修改为 5s
            this.editor.config.height = noteEditSpaceHeight;
            // 取消自动 focus
            this.editor.config.focus = false
            this.editor.config.onchange = (newHtml: any)=> this.editorOnChange(newHtml);//内容改变触发
            this.editor.config.onblur = (newHtml: any) => this.editorOnBlur(newHtml);//失去焦点后触发
            this.editor.config.onfocus = (newHtml:any) => this.editorOnfocus(newHtml);//聚焦触发
            this.editor.create();
        });
    }
    editorOnChange(newHtml: string){}
    editorOnBlur(newHtml: string){}
    editorOnfocus(newHtml: string){}
    //随机生成指定数量的32进制key
    generatekey(): string {
        const num = 32;
        let library = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        let key = "";
        for (let i = 0; i < num; i++) {
            let randomPoz = Math.floor(Math.random() * library.length);
            key += library.substring(randomPoz, randomPoz + 1);
        }
        return key;
    }
    //DES加密
    encryptDes(message: string, key: string): string {
        const keyHex = cryptoJs.enc.Utf8.parse(key)
        const option = { mode: cryptoJs.mode.ECB, padding: cryptoJs.pad.Pkcs7 }
        const encrypted = cryptoJs.DES.encrypt(message, keyHex, option)
        return encrypted.ciphertext.toString()
    }
    //DES解密
    decryptDes(message: string, key: string): string {
        const keyHex = cryptoJs.enc.Utf8.parse(key)
        const decrypted = cryptoJs.DES.decrypt(
            {
                ciphertext: cryptoJs.enc.Hex.parse(message)
            },
            keyHex,
            {
                mode: cryptoJs.mode.ECB,
                padding: cryptoJs.pad.Pkcs7
            }
        )
        return decrypted.toString(cryptoJs.enc.Utf8)
    }
    //加密des消息
    encodeDesMessage(message: any): string {
        const base64EncodeCont = Base64.encode(message);
        const key = this.generatekey();
        const hasDESEncrypt = key.substring(0,16) + this.encryptDes(base64EncodeCont, key) + key.substring(16,32);
        return hasDESEncrypt;
    }
    //解密消息
    decodeDesMessage(hasEncodeContent: string): string{
        const key = hasEncodeContent.substring(0,16) + hasEncodeContent.substring(hasEncodeContent.length - 16, hasEncodeContent.length);
        const desEncodeMess = hasEncodeContent.substring(16, hasEncodeContent.length - 16);
        const base64EncodeMess = this.decryptDes(desEncodeMess,key);
        const htmlCont = Base64.decode(base64EncodeMess);
        return htmlCont;
    }
    
}